<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
      xmlns:sec="http://www.thymeleaf.org/extras/spring-security"
      layout:decorate="~{layout/base}">
<head>
    <title th:text="${category} + ' - 暖心助农选品平台'">分类 - 暖心助农选品平台</title>
</head>
<body>
    <div layout:fragment="content">
        <div class="row mb-4">
            <div class="col">
                <h2>农产品分类</h2>
                <div class="list-group">
                    <a th:href="@{/}" class="list-group-item list-group-item-action">全部</a>
                    <a th:href="@{/products/category/vegetables}" 
                       class="list-group-item list-group-item-action"
                       th:classappend="${category == 'vegetables'} ? 'active' : ''">蔬菜</a>
                    <a th:href="@{/products/category/fruits}"
                       class="list-group-item list-group-item-action"
                       th:classappend="${category == 'fruits'} ? 'active' : ''">水果</a>
                    <a th:href="@{/products/category/grains}"
                       class="list-group-item list-group-item-action"
                       th:classappend="${category == 'grains'} ? 'active' : ''">粮食</a>
                    <a th:href="@{/products/category/others}"
                       class="list-group-item list-group-item-action"
                       th:classappend="${category == 'others'} ? 'active' : ''">其他</a>
                </div>
            </div>
        </div>

        <div class="row">
            <div class="col">
                <h2 th:text="${category == 'vegetables' ? '蔬菜' : 
                             category == 'fruits' ? '水果' : 
                             category == 'grains' ? '粮食' : '其他'} + '类农产品'">
                    分类农产品
                </h2>
                <div class="row row-cols-1 row-cols-md-3 g-4">
                    <div class="col" th:each="product : ${products}">
                        <div class="card h-100">
                            <img th:if="${product.image}" th:src="@{${product.image}}" class="card-img-top" alt="产品图片">
                            <img th:unless="${product.image}" th:src="@{/images/default-product.jpg}" class="card-img-top" alt="默认产品图片">
                            <div class="card-body">
                                <h5 class="card-title" th:text="${product.name}">产品名称</h5>
                                <p class="card-text" th:text="${product.description}">产品描述</p>
                                <p class="card-text">
                                    <small class="text-muted" th:text="'类别: ' + ${product.category}">类别</small>
                                </p>
                                <p class="card-text">
                                    <strong class="text-danger" th:text="'￥' + ${product.price}">价格</strong>
                                </p>
                                <div sec:authorize="hasRole('EMPLOYEE')">
                                    <form th:action="@{/cart/add}" method="post" class="d-flex align-items-center">
                                        <input type="hidden" name="productId" th:value="${product.id}">
                                        <input type="number" name="quantity" value="1" min="1" th:max="${product.stock}" 
                                               class="form-control form-control-sm me-2" style="width: 80px;">
                                        <button type="submit" class="btn btn-primary btn-sm">
                                            加入购物车
                                        </button>
                                    </form>
                                </div>
                                <p class="card-text mt-2">
                                    <small class="text-muted" th:text="'库存: ' + ${product.stock}">库存</small>
                                </p>
                            </div>
                        </div>
                    </div>
                </div>

                <div th:if="${products.empty}" class="alert alert-info mt-4">
                    该分类下暂无商品。
                </div>
            </div>
        </div>
    </div>

    <th:block layout:fragment="scripts">
        <script th:inline="javascript">
            $(document).ready(function() {
                $('form').on('submit', function(e) {
                    e.preventDefault();
                    var form = $(this);
                    $.post(form.attr('action'), form.serialize())
                        .done(function(response) {
                            if (response === 'success') {
                                alert('已添加到购物车');
                            } else {
                                alert('添加失败: ' + response);
                            }
                        })
                        .fail(function() {
                            alert('添加失败');
                        });
                });
            });
        </script>
    </th:block>
</body>
</html> 